package domain;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.format.annotation.DateTimeFormat;

@Entity
@Access(AccessType.PROPERTY)
public class Comment extends DomainEntity {
	//Constructors

		public Comment() {
			super();
		children = new HashSet<Comment>();
		}
		

		//Attributes
		private Date creationMoment;
		private String text;
		
		@NotNull
		@Past
		@Temporal(TemporalType.TIMESTAMP)
		@DateTimeFormat(pattern = "dd/MM/yyyy HH:mm")
		public Date getCreationMoment() {
			return creationMoment;
		}
		public void setCreationMoment(Date creationMoment) {
			this.creationMoment = creationMoment;
		}
		
		@NotBlank
		public String getText() {
			return text;
		}
		public void setText(String text) {
			this.text = text;
		}
		
		// Relationships
		
		private Customer customer;
		private Painting painting;
		private Collection<Comment> children;
		private Comment parent;

		@Valid
		@NotNull
		@ManyToOne(optional = false)
		public Customer getCustomer() {
			return customer;
		}

		public void setCustomer(Customer customer) {
			this.customer = customer;
		}
		
		

		@Valid
		@NotNull
		@ManyToOne(optional = false)
		public Painting getPainting() {
			return painting;
		}

		public void setPainting(Painting painting) {
			this.painting = painting;
		}
		@OneToMany(mappedBy = "parent")
		public Collection<Comment> getChildren(){
			return children;
		}
		
		public void setChildren(Collection<Comment> children){
			this.children = children;
		}
		
		@Valid
		@ManyToOne(optional = true)
		public Comment getParent(){
			return parent;
		}
		
		public void setParent(Comment parent){
			this.parent = parent;
		}
}
